package com.show.web.bigscreen;

import com.show.HelpClazz;
import com.show.common.Result;
import com.show.dto.Trend;
import com.show.dto.population.Population;
import com.show.dto.sensor.GasSensor;
import com.show.dto.sensor.GasSensorError;
import com.show.dto.sensor.ShaftCoverSensor;
import com.show.dto.sensor.ShaftCoverSensorError;
import com.show.service.population.PopulationService;
import com.show.service.sensor.GasSensorErrorService;
import com.show.service.sensor.GasSensorService;
import com.show.service.sensor.ShaftCoverSensorErrorService;
import com.show.service.sensor.ShaftCoverSensorService;
import com.show.util.HelperClazz;
import com.show.util.ResultUtil;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 大屏展示数据控制器
 *
 * @author liukai
 */
@RestController
@RequestMapping(value = "bigScreen")
public class BigScreenDataController {

    private PopulationService populationService;

    private ShaftCoverSensorService shaftCoverSensorService;

    private ShaftCoverSensorErrorService shaftCoverSensorErrorService;

    private GasSensorService gasSensorService;

    private GasSensorErrorService gasSensorErrorService;

    @Autowired
    public BigScreenDataController(PopulationService populationService, ShaftCoverSensorService shaftCoverSensorService, ShaftCoverSensorErrorService shaftCoverSensorErrorService, GasSensorService gasSensorService, GasSensorErrorService gasSensorErrorService) {
        this.populationService = populationService;
        this.shaftCoverSensorService = shaftCoverSensorService;
        this.shaftCoverSensorErrorService = shaftCoverSensorErrorService;
        this.gasSensorService = gasSensorService;
        this.gasSensorErrorService = gasSensorErrorService;
    }


    /**
     * 井盖状态监测接口
     *
     * @author liukai
     */
    @GetMapping("holecount")
    public void holecount(HttpServletResponse response) {
        final String url = "http://10.64.8.209:8800/holeinter/screen/holecount";
        String json = HelpClazz.sendHttpPost(url);
        HelperClazz.renderJson(response, json);
    }


    /**
     * 井室有害气体监测
     */
    @GetMapping("manholeCount")
    public void manholeCount(HttpServletResponse response) {
        final String url = "http://10.64.8.209:8800/holeinter/screen/manholeCount";
        String json = HelpClazz.sendHttpPost(url);
        HelperClazz.renderJson(response, json);
    }


    /**
     * 设备警告信息
     */
    @GetMapping("manholewarning")
    public void manholewarning(HttpServletResponse response) {
        final String url = "http://10.64.8.209:8800/holeinter/screen/manholewarning";
        String json = HelpClazz.sendHttpPost(url);
        HelperClazz.renderJson(response, json);
    }

    /**
     * 井口信息
     */
    @GetMapping("manholeinfo")
    public void manholeinfo(HttpServletResponse response) {
        final String url = "http://10.64.8.209:8800/holeinter/screen/manholeinfo";
        String json = HelpClazz.sendHttpPost(url);
        HelperClazz.renderJson(response, json);
    }

    /**
     * 井盖传感器故障率
     */
    @GetMapping("holefailurerate")
    public void holefailurerate(HttpServletResponse response) {
        final String url = "http://10.64.8.209:8800/holeinter/screen/holefailurerate";
        String json = HelpClazz.sendHttpPost(url);
        HelperClazz.renderJson(response, json);
    }


    /**
     * 有害气体传感器故障率
     */
    @GetMapping("gasholefailurerate")
    public void gasholefailurerate(HttpServletResponse response) {
        final String url = "http://10.64.8.209:8800/holeinter/screen/gasholefailurerate";
        String json = HelpClazz.sendHttpPost(url);
        HelperClazz.renderJson(response, json);
    }


    /**
     * 物联网部件
     */
    @GetMapping("holeparts")
    public void holeparts(String sensorId, String status, String dataTime, HttpServletResponse response) {
        String url = "http://10.64.8.209:8800/holeinter/screen/holeparts?sensorId=" + sensorId + "&status=" + status + "&dataTime=" + dataTime + "";
        url = url.replaceAll(" ", "%20");
        String json = HelpClazz.sendHttpGet(url);
        HelperClazz.renderJson(response, json);
    }

    /**
     * 人口流量(右上)
     *
     * @author 姜文
     */
    @GetMapping("findPopulation")
    public Result findPopulation() {
        List<Population> populationList = populationService.findPopulation();
        return ResultUtil.success("人口流量", populationList);
    }

    /**
     * 物联设备状态--井盖传感器故障率(右中左)
     *
     * @author 姜文
     */
    @GetMapping("shaftCoverSensorPercentage")
    public Result shaftCoverSensorPercentage() {
        Double total;
        List<ShaftCoverSensor> shaftCoverSensorList = shaftCoverSensorService.shaftCoverSensorTotal();
        List<ShaftCoverSensorError> shaftCoverSensorErrorList = shaftCoverSensorErrorService.shaftCoverSensorErrorTotal();
        double scs = shaftCoverSensorList.size();
        double scse = shaftCoverSensorErrorList.size();
        total = scse / scs;
        return ResultUtil.success("井盖传感器故障率", total);
    }

    /**
     * 物联设备状态--有害气体传感器故障率(右中右)
     *
     * @author 姜文
     */
    @GetMapping("gasSensorPercentage")
    public Result gasSensorPercentage() {
        Double total;
        List<GasSensor> gasSensorList = gasSensorService.gasSensorTotal();
        List<GasSensorError> gasSensorErrorList = gasSensorErrorService.gasSensorErrorTotal();
        double scs = gasSensorList.size();
        double scse = gasSensorErrorList.size();
        total = scse / scs;
        return ResultUtil.success("有害气体传感器故障率", total);
    }

    /**
     * 设备告警信息--井盖传感器故障事件(右下井盖)
     *
     * @author 姜文
     */
    @GetMapping("shaftCoverSensorEvent")
    public Result shaftCoverSensorEvent() {
        List<ShaftCoverSensorError> shaftCoverSensorErrorList = shaftCoverSensorErrorService.shaftCoverSensorEvent();
        return ResultUtil.success("井盖传感器故障事件", shaftCoverSensorErrorList);
    }

    /**
     * 设备告警信息--有害气体传感器故障事件(右下毒气)
     *
     * @author 姜文
     */
    @GetMapping("gasSensorEvent")
    public String gasSensorEvent() {
        final String url = "http://10.64.8.209:8800/holeinter/screen/manholewarning";
        try {
            return post(url);
        } catch (IOException e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    /**
     * 井盖传感器故障率-大屏展示-点击事件(右中左-点击)
     *
     * @author 姜文
     */
    @GetMapping("shaftCoverSensorError")
    public Result shaftCoverSensorError() {
        List<ShaftCoverSensorError> shaftCoverSensorErrorList = shaftCoverSensorErrorService.shaftCoverSensorError();
        return ResultUtil.success("井盖传感器故障率-大屏展示-点击事件", shaftCoverSensorErrorList);
    }

    /**
     * 有毒气体传感器故障率-大屏展示-点击事件(右中右-点击)
     *
     * @author 姜文
     */
    @GetMapping("gasSensorError")
    public Result gasSensorError() {
        List<GasSensorError> gasSensorErrorList = gasSensorErrorService.gasSensorError();
        return ResultUtil.success("有毒气体传感器故障率-大屏展示-点击事件", gasSensorErrorList);
    }

    /**
     * 井盖传感器故障事件-大屏展示-点击事件(右下左-点击)
     *
     * @author 姜文
     */
    @GetMapping("shaftCoverSensorErrorEvent")
    public Result shaftCoverSensorErrorEvent(String id) {
        List<ShaftCoverSensorError> shaftCoverSensorErrorList = shaftCoverSensorErrorService.shaftCoverSensorErrorEvent(id);
        return ResultUtil.success("井盖传感器故障事件-大屏展示-点击事件", shaftCoverSensorErrorList);
    }

    /**
     * 有毒气体传感器故障事件-大屏展示-点击事件(右下右-点击)
     *
     * @author 姜文
     */
    @GetMapping("gasSensorErrorEvent")
    public Result gasSensorErrorEvent(String id) {
        List<GasSensorError> gasSensorErrorList = gasSensorErrorService.gasSensorErrorEvent(id);
        return ResultUtil.success("有毒气体传感器故障事件-大屏展示-点击事件", gasSensorErrorList);
    }

    /**
     * 物联网部件接口
     *
     * @param smId 部件smid
     * @return Result
     * @author liukai
     */
    @PostMapping("getUnit")
    public String getUnit(String smId) {
        final String url = "http://10.64.8.209:8800/holeinter/screen/manholeinfo";
        try {
            return post(url);
        } catch (IOException e) {
            e.printStackTrace();
            return e.getMessage();
        }
//        try {
//            Unit unit = unitService.getUnitBySmId(smId);
//            return ResultUtil.success("查询成功!", unit);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return ResultUtil.error("查询物联网部件后台出现错误!", e.getMessage());
//        }
    }

    public String post(String url) throws IOException {
        return Jsoup.connect(url).header("Accept", "*/*").header("Accept-Encoding", "gzip, deflate").header("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3").header("Content-Type", "application/json;charset=UTF-8").header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0").timeout(10000).ignoreContentType(true).execute().body();
    }

    /**
     * 井盖状态监测接口
     *
     * @author liukai
     */
    @GetMapping("shaftStatusMonitor")
    public void shaftStatusMonitor(HttpServletResponse response) {
        final String url = "http://10.64.8.209:8800/holeinter/screen/holecount";
        String json = HelpClazz.sendHttpPost(url);
        HelperClazz.renderJson(response, json);
    }


    /**
     * 井室有害气体监测
     */
    @GetMapping("gasSensorMonitor")
    public void gasSensorMonitor(HttpServletResponse response) {
        final String url = "http://10.64.8.209:8800/holeinter/screen/manholeCount";
        String json = HelpClazz.sendHttpPost(url);
        HelperClazz.renderJson(response, json);
    }


    /**
     * 发展态势接口
     *
     * @author liukai
     */
    @GetMapping("queryTrend")
    public Result queryTrend() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        List<String> dayList = getDayList();

        Map<String, Object> map1 = new HashMap<String, Object>();
        Map<String, Object> map2 = new HashMap<String, Object>();
        Map<String, Object> map4 = new HashMap<String, Object>();
        /*
        震动
         */
        List<Trend> trendList = new ArrayList<Trend>();
        for (String dateString : dayList) {
            Trend trend = new Trend();
            int count = shaftCoverSensorService.queryCount(dateString, "1");
            trend.setDate(dateString.substring(5, dateString.length()));
            trend.setCount(count);
            trendList.add(trend);
        }
        map1.put("trend1", trendList);
        list.add(map1);


        /*
        溢水
         */
        List<Trend> trendList2 = new ArrayList<Trend>();
        for (String dateString : dayList) {
            Trend trend = new Trend();
            int count = shaftCoverSensorService.queryCount(dateString, "2");
            trend.setDate(dateString.substring(5, dateString.length()));
            trend.setCount(count);
            trendList2.add(trend);
        }
        map2.put("trend2", trendList2);
        list.add(map2);


        /*
        倾角
         */
        List<Trend> trendList4 = new ArrayList<Trend>();
        for (String dateString : dayList) {
            Trend trend = new Trend();
            int count = shaftCoverSensorService.queryCount(dateString, "4");
            trend.setDate(dateString.substring(5, dateString.length()));
            trend.setCount(count);
            trendList4.add(trend);
        }
        map4.put("trend4", trendList4);
        list.add(map4);

        return ResultUtil.success("发展态势接口数据查询成功!", list);

    }


    private List<String> getDayList() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        List<String> list = new ArrayList<String>();
        Date date = new Date();
        for (int i = 0; i < 10; i++) {
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            date = calendar.getTime();
            String dateString = formatter.format(date);
            list.add(dateString);
        }
        Collections.reverse(list);
        return list;
    }

    /**
     * 大屏事件
     *
     * @author 姜文
     */
    @GetMapping("getEvent")
    public String getEvent() {
        final String url = "http://10.64.8.209:8800/holeinter/screen/manholewarning";
        try {
            return post(url);
        } catch (IOException e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }
}
